package com.jwd.avmanager.test;

import com.alibaba.fastjson.JSON;

import com.jwd.avmanager.pojo.Streams;
import com.jwd.avmanager.pojo.StreamsAddDate;
import com.jwd.avmanager.pojo.StreamsInfo;
import com.jwd.avmanager.server.ClientManager;
import com.jwd.avmanager.util.DebugLogger;
import com.jwd.avmanager.util.HttpRequestUtils;
import com.jwd.avmanager.util.JodaTimeUtil;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


public class Httptest {
    public static void main(String[] args) throws IOException {
        String  url = "http://47.119.165.185:1985/api/v1/streams?start=0";
        String json = HttpRequestUtils.sendGetRequest(url);
//        DebugLogger.debug(json);
        StreamsInfo streamsInfo = JSON.parseObject(json, StreamsInfo.class);
//        DebugLogger.debug(streamsInfo);
        List<Streams> streams = streamsInfo.getStreams();
        if(streams == null || streams.size() == 0){
            ClientManager.getInstance().clestreamsAddDateMap();
        } else {
            for (Streams stream : streams) {
                DebugLogger.info(stream.toString());
                DebugLogger.info(stream.getClients());
                StreamsAddDate streamsAddDate1 = ClientManager.getInstance().getstreamsAddDateMap(stream.getId());
                if(streamsAddDate1 == null){
                    StreamsAddDate streamsAddDate = new StreamsAddDate();
                    streamsAddDate.setStreams(stream);
                    streamsAddDate.setDate(new Date());
                    ClientManager.getInstance().addstreamsAddDateMap(stream.getId(),streamsAddDate);
                } else {
                    Streams streams1 = streamsAddDate1.getStreams();

                    if( streams1.getClients() != stream.getClients() ){
                        StreamsAddDate streamsAddDate = new StreamsAddDate();
                        streamsAddDate.setStreams(stream);
                        streamsAddDate.setDate(new Date());
                        ClientManager.getInstance().addstreamsAddDateMap(stream.getId(),streamsAddDate);
                    }
                }

            }
        }
        int size = ClientManager.getInstance().sizestreamsAddDateMap();
        if(size != 0){
            ConcurrentHashMap<String, StreamsAddDate> map = ClientManager.getInstance().get();
            for (Map.Entry<String, StreamsAddDate> entry : map.entrySet()) {
                DebugLogger.info(entry.getKey() + ":" + entry.getValue());
                StreamsAddDate value = entry.getValue();
                Streams streams1 = value.getStreams();
                long s = JodaTimeUtil.timeDifference(value.getDate());
                if(s>(5*60) && streams1.getClients()<=1){
                    ClientManager.getInstance().delstreamsAddDateMap(streams1.getId());
                }

            }

        }


    }
}
